package com.kfm.servlet;

import com.kfm.util.Contains;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求转发
        req.getRequestDispatcher("login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");


        // TODO (去数据库查询)
        if ("admin".equals(username) && "123456".equals(password)){
            getServletContext().setAttribute(Contains.LOGIN_FLAG, "admin");
            resp.sendRedirect("index");
        } else{
            resp.sendRedirect("login.jsp");
        }


    }
}
